package com.example.Adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.shopapp.R;
import com.example.viewmodel.Coupon;

import java.util.List;

public class CouponAdapter extends RecyclerView.Adapter<CouponAdapter.ViewHolder> {
    private Context ctx;//上下文对象
    private List<Coupon> coupons;

    public CouponAdapter(Context ctx, List<Coupon> coupons) {
        this.ctx = ctx;
        this.coupons = coupons;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { //获取视图
        View view = LayoutInflater.from(this.ctx).inflate(R.layout.home_coupon_item, parent, false);
        ViewHolder viewHolder = new ViewHolder(view);

        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) { //数据渲染
        holder.home_coupon_discount.setText(this.coupons.get(position).getDiscount()+"");
        holder.home_coupon_name.setText(this.coupons.get(position).getName()+"");
        holder.home_coupon_tag.setText(this.coupons.get(position).getTag()+"");
        holder.home_coupon_days.setText(this.coupons.get(position).getDays()+"");
    }

    @Override
    public int getItemCount() {
        return this.coupons.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder{
        public TextView home_coupon_discount;
        public TextView home_coupon_name;
        public TextView home_coupon_tag;
        public TextView home_coupon_days;
        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            this.home_coupon_discount =itemView.findViewById(R.id.home_coupon_discount);
            this.home_coupon_name = itemView.findViewById(R.id.home_coupon_name);
            this.home_coupon_tag = itemView.findViewById(R.id.home_coupon_tag);
            this.home_coupon_days = itemView.findViewById(R.id.home_coupon_days);
        }
    }
}
